package org.gxuwz.core.dao.impl;

import java.util.List;

import org.gxuwz.core.dao.IChrDisSynDao;
import org.gxuwz.core.entity.TChronicdisz;
import org.gxuwz.core.entity.TFamily;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("ChrDisSynDao")
public class ChrDisSynDao extends BaseDao implements IChrDisSynDao{
	@Autowired
	private SessionFactory sessionFactory;
	public Session getSession() {
		Session session = sessionFactory.openSession();
		return session;
	}
	@Override							//添加家庭档案信息		
	public void addChrDisSynDao(TChronicdisz chrdissyn) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(chrdissyn);
		tx.commit();
		session.close();
		
	}
	//@Override							//查询家庭档案信息
	@SuppressWarnings("unchecked")
	public List<TChronicdisz> getAll(String chrdissynid, String perscode) {
		List<TChronicdisz> list = null;
		Session session = getSession();
		String hql = "select * from t_chronicdisz where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(perscode!=null&&chrdissynid!=""){
			hql += " and perscode like '%"+perscode+"%'";
		}
		if(chrdissynid!=null&&chrdissynid!=""){
			hql += " and chrdissynid like '%"+chrdissynid+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TChronicdisz.class);
		//Query query = session.createQuery(hql);
		list = query.list();
		session.close();
		return list;
		
	}
	
	@Override							
	public TChronicdisz getByPersCode(String perscode) {
		List<TChronicdisz> list = null;
		Session session = getSession();
		String hql = "select * from t_chronicdisz where 1=1";
		if(perscode!=null&&perscode!=""){
			hql += " and perscode like '%"+perscode+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TChronicdisz.class);
		list = query.list();
		session.close();
		return list.get(0);
	}
	@Override
	public void update(TChronicdisz chrdissyn) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		//session.merge(chrdissyn);
		session.update(chrdissyn);
		tx.commit();
		session.close();
		
	}
	@Override
	public void delete(TChronicdisz chrdissyn) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(chrdissyn);
		tx.commit();
		session.close();
		
		
	}
	
	
}
